define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wanlshop/lifeend/index' + location.search,
                    add_url: 'wanlshop/lifeend/add',
                    edit_url: 'wanlshop/lifeend/edit',
                    del_url: 'wanlshop/lifeend/del',
                    multi_url: 'wanlshop/lifeend/multi',
                    import_url: 'wanlshop/lifeend/import',
                    table: 'wanlshop_lifeend',
                    examine_url: 'wanlshop/lifeend/examine',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        // {field: 'data', title: __('Data')},
                        {field: 'status', title: __('Status'), searchList: {"created":__('Created'),"successed":__('Successed'),"rejected":__('Rejected')}, formatter: Table.api.formatter.status},
                        {field: 'payment_status', title: __('Payment_status'), searchList: {"0":__('Payment_status 0'),"1":__('Payment_status 1'),"2":__('Payment_status 2'),"3":__('Payment_status 3'),"4":__('Payment_status 4')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,  formatter: function (value, row, index) {
                            var that = $.extend({}, this);
                            var table = $(that.table).clone(true);
                            //隐藏删除按钮
                            $(table).data('operate-del', null);
                            $(table).data('operate-edit', null);
                            //根据状态判断是否要显示该按钮
                            
                            that.table = table;
                            return Table.api.formatter.operate.call(that, value,row,index)
                        },
                        buttons: [
                            {
                                name:'info',
                                text:'详情',
                                icon: 'fa fa-',
                                classname: 'btn btn-xs btn-primary btn-dialog', //btn-dialog 弹出窗
                                url: (row) => {
                                    return "wanlshop/lifeend/info?id="+row.id 
                                }, //url 地址 这里是个用户列表
                                visible: function (row) {
                                    //true：显示 false：不显示 可以row自定义显示
                                    // if (row.status == 'created') return true;
                                    return true;
                                }
                            },
                            {
                                name:'examine',
                                text:'审核',
                                icon: 'fa fa-',
                                classname: 'btn btn-xs btn-primary btn-dialog', //btn-dialog 弹出窗
                                url: (row) => {
                                    return "wanlshop/lifeend/examine?id="+row.id 
                                }, //url 地址 这里是个用户列表
                                visible: function (row) {
                                    //true：显示 false：不显示 可以row自定义显示
                                    if (row.status == 'created') return true;
                                    return false;
                                }
                            },    
                            {
                                name:'payment',
                                text:'标记付款',
                                icon: 'fa fa-',
                                classname: 'btn btn-xs btn-primary btn-ajax', //
                                confirm: '是否标记为已打款',
                                // btn-dialog 弹出窗
                                url: (row) => {
                                    return "wanlshop/lifeend/payment?id="+row.id 
                                }, //url 地址 这里是个用户列表
                                visible: function (row) {
                                    //true：显示 false：不显示 可以row自定义显示
                                    if (row.status == 'successed' && row.payment_status == '0') return true;
                                    return false;
                                },
                                success: function (data, rest) {
                                    //刷新当前页面
                                    table.bootstrapTable('refresh', {});
                                    return true;
                                }
                            },    
                        ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        examine: function () {

            $('button[type="submit"]').click(function(event) {
                // console.log(res.target)

                $('#c-status').val($(event.target).attr('v'))
                // return false
            })

            Form.api.bindevent($("form[role=form]"), function (data, ret) {

                Toastr.success("成功");
            }, function (data, ret) {
                // Toastr.success("失败");
                // return false;
            }, function (success, error) {
                let _this = this;
                console.log(_this);
                layer.confirm("确认" + ($('#c-status').val() == 'successed'?'通过':'驳回') + '审核信息?', { icon: 3, title: '提示' }, function (index) {
                    
                    Form.api.submit(_this, success, error);
                    return false;
                    //do something
    
                });
                return false;
    
            });
            
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
